package com.jl.model.socket;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.jl.enums.GameType;
import com.jl.model.niuniu.enums.AdvancedOptions;
import com.jl.utils.niuniu.GlobalData;
import io.netty.channel.ChannelHandlerContext;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.java_websocket.client.WebSocketClient;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CountDownLatch;

@Data
@Slf4j
@JsonIgnoreProperties({"hibernateLazyInitializer","handler"})
public class RoomDetail {

    private Map<Integer, Object> userInfo = new HashMap<>();   //key=userId,value=用户详情

    private MyChannelHandlerPool player = new MyChannelHandlerPool();  //准备用户组

    private MyChannelHandlerPool watcher = new MyChannelHandlerPool();  //观战用户组

    private Map<Integer, ChannelHandlerContext> userChanel = new HashMap<>(); //key=userId 玩家

    private Map<Integer, ChannelHandlerContext> userChanelWatcher = new HashMap<>(); //观战用户

    private ConcurrentHashMap<Integer,Integer> userSeat = new ConcurrentHashMap<>(); //用户对应的座位

    private Map<Integer, WebSocketClient> userTrusteeship = new HashMap<>();

    private Map<Integer, Thread> userTrusteeshipThread = new HashMap<>();

    private GlobalData globalData;

    private String status = "0";

    private CountDownLatch countDown = new CountDownLatch(0);
    private boolean countDownSwitch = false;
    private Thread currentThread = null;

    public RoomDetail() {

    }

    public GlobalData getGlobalData(String adv, GameType gameType) {
        log.info("globalData是{}",globalData);
        log.info("房间高级选项是:{}",adv);
        if (globalData==null){ //第一次生成
            genGlobalData(adv, gameType);
        }
        log.info("牌库有{}张牌!!!",globalData.getGlobalData().length);
        return globalData;
    }

    private void genGlobalData(String adv, GameType gameType) {
        if (gameType.getId().equals("1")){ //牛牛
            if (adv.contains(AdvancedOptions.type5.getId())){ //如果是王癞
                globalData = new GlobalData(2);
            }else{
                globalData = new GlobalData(1);
            }
        }else { //斗公牛
            if (adv.contains(com.jl.model.gongniu.enums.AdvancedOptions.type5.getId())){ //有花牌
                globalData = new GlobalData(1);
            }else{ //无花牌
                globalData = new GlobalData(3);
            }
        }
    }

    public void init(String adv,GameType gameType){
        log.info("房间高级选项是:{}",adv);
        log.info("getCardNum{}",globalData.getCardNum());
        genGlobalData(adv,gameType);
    }

    /*
    断线重连后, 移除用户以前的连接,并设置新连接
     */
    public void reConnect(Integer userId,ChannelHandlerContext newChannel){
        ChannelHandlerContext channelHandlerContext = userChanel.get(userId);
        if (channelHandlerContext!=null){
            player.getChannelGroup().remove(channelHandlerContext.channel());
            player.getChannelGroup().add(newChannel.channel());
            channelHandlerContext.close();
        }
        userChanel.put(userId,newChannel);
        if (userTrusteeship.get(userId)!=null){
            userTrusteeship.get(userId).close();
            userTrusteeship.remove(userId);
        }
        if (userTrusteeshipThread.get(userId)!=null){
            userTrusteeshipThread.get(userId).interrupt();
            userTrusteeshipThread.remove(userId);
        }
    }

    public void begin(int time){
        currentThread = new Thread(new Runnable() {
            @Override
            public void run() {
                if (!countDownSwitch){
                    countDown = new CountDownLatch(time);
                    countDownSwitch = true;
                }else {
                    System.out.println("???");
                    return;
                }
                while (countDown.getCount()!=0 && true == countDownSwitch){
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        log.info("中止倒计时");
                    }
                    countDown.countDown();
                    System.out.println(countDown);
                }
                countDownSwitch = false;
                return;
            }
        });
        currentThread.start();
    }

    public void interrupt(){
        if(countDownSwitch){
            this.getCurrentThread().interrupt();
            countDownSwitch = false;
            countDown = new CountDownLatch(0);
        }
    }
}
